package com.kygeng.datamock.util;



import com.kygeng.common.model.database.CompanyLicense;
import com.kygeng.common.model.database.CompanyRisk;
import com.kygeng.common.model.database.CompanyTags;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Title: MockComapnyTagsUtil
 * @Author: kygeng
 * @Package: com.kygeng.datamonitior.util
 * @Date: 2024/9/4 20:47
 * @Description:
 */
public class MockCompanyTagsUtil {

    private static final String[] tags = {"高新技术企业", "科技小巨人企业", "科技型中小企业", "初创科技型企业", "专精特新小巨人企业", "重点专精特新小巨人企业", "专精特新中小企业", "创建世界一流示范企业", "创建世界一流专精特新示范企业", "星火计划企业", "火炬计划企业", "国家软科学研究计划企业", "民营科技企业", "技术先进型服务企业", "瞪羚企业", "雏鹰企业", "科技服务体系企业", "企业技术中心", "双百企业", "科改企业", "特色企业名录", "发债企业", "金融机构", "活跃融资企业", "上市企业", "中华老字号", "创新型企业", "创新型中小企业", "创新型产业集群企业", "隐形冠军企业",
            "国家重点新产品计划企业", "PEVC被投企业", "制造业单项冠军企业", "制造业单项冠军示范企业", "制造业单项冠军培育企业", "制造业单项冠军产品企业", "军民融合企业", "双软企业", "联合国责任投资原则组织全球企业", "联合国责任投资原则组织国内企业",
            "知识产权优势企业", "知识产权示范企业", "企业工业设计中心", "工业设计企业", "机电产品再制造试点单位", "国企名录", "国资委实际控股企业", "国资委直接参股企业", "高校名录", "教育部直属高校", "双一流院校", "双高院校", "985工程院校", "211工程院校", "101计划高校", "绿色名录",
            "绿色工厂", "绿色供应链管理企业", "全国碳排放权交易配额管理企业", "重点用能行业能效领跑者", "央企国企", "中央企业", "中央部门和单位所属企业", "地方国有及国有控股企业"};

    private static final String[] risk = {"经营异常", "失信信息", "行政处罚", "严重违法", "欠税信息", "裁判文书", "开庭公告", "有司法协助", "有司法拍卖", "动产抵押", "股权出质", "知识产权出质"};

    private static Map<String, List<String>> licenseMap = new HashMap<>();
    private static List<String> licenseList = Arrays.asList("行政许可", "电信许可", "金融行为许可", "建筑行业资质");

    static {
        licenseMap.put("行政许可", Arrays.asList("网络文化经营许可证", "人防工程平时使用许可决定书", "生活饮用水卫生行政许可", "行政许可决定书", "网络文化经营许可证", "市场监督管理行政许可"));
        licenseMap.put("电信许可", Arrays.asList("信息服务业务", "互联网信息服务", "在线数据处理", "在线交易", "通信服务业务", "互联网数据中心业务", "蜂窝移动通信业务"));
        licenseMap.put("金融行为许可", Arrays.asList("经营证券期货业务许可", "证券投资咨询业务资格", "证券自营业务许可", "证券经纪业务许可"));
        licenseMap.put("建筑行业资质", Arrays.asList("建筑业企业资质", "勘查资质", "监理资质"));
    }

    public static CompanyTags getRandomTags(String companyCode) {
        Random random = new Random();
        int numTagsToPick = random.nextInt(6); // [0, 1, 2, 3, 4, 5]

        List<String> selectedTags = new ArrayList<>();
        for (int i = 0; i < numTagsToPick; i++) {
            int index = random.nextInt(tags.length);
            selectedTags.add(tags[index]);
        }

        CompanyTags res = new CompanyTags();
        res.setCompanyCode(companyCode);
        res.setTags(String.join(",", selectedTags));
        return res;
    }


    public static List<CompanyLicense> getRandomLicense(String companyCode) {
        Random random = new Random();
        ArrayList<CompanyLicense> result = new ArrayList<>();
        int times = random.nextInt(4);
        for (int i = 0; i < times; i++) {

            CompanyLicense companyLicense = new CompanyLicense();
            companyLicense.setCompanyCode(companyCode);

            int index = new Random().nextInt(licenseList.size());
            String licenseType = licenseList.get(index);
            List<String> licenseNameList = licenseMap.get(licenseType);

            int licenseNameIndex = new Random().nextInt(licenseNameList.size());
            String licenseName = licenseNameList.get(licenseNameIndex);

            String licenseNo = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, 8);

            String randomDate = getRandomDate();
            String endData = getEndData(randomDate, 20);

            companyLicense.setLicenseNo(licenseNo);
            companyLicense.setLicenseType(licenseType);
            companyLicense.setLicenseName(licenseName);
            companyLicense.setStartDate(randomDate);
            companyLicense.setEndDate(endData);
            result.add(companyLicense);

        }
        return result;
    }

    public static List<CompanyRisk> getRandomRisk(String companyCode) {
        Random random = new Random();
        ArrayList<CompanyRisk> result = new ArrayList<>();
        int times = random.nextInt(2);
        for (int i = 0; i < times; i++) {
            int index = random.nextInt(risk.length);
            String riskType = risk[index];
            String riskCode = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, 8);
            String riskDetail = "";

            CompanyRisk companyRisk = new CompanyRisk();
            companyRisk.setCompanyCode(companyCode);
            companyRisk.setRiskCode(riskCode);
            companyRisk.setRiskType(riskType);
            companyRisk.setRiskDetail(riskDetail);
            result.add(companyRisk);
        }
        return result;
    }


    public static String getRandomDate() {
        // 设置日期范围
        LocalDate startDate = LocalDate.of(2000, 1, 1);
        LocalDate endDate = LocalDate.of(2024, 8, 30);

        // 生成随机日期
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        Random random = new Random();
        long randomDays = random.nextInt((int) daysBetween + 1);
        LocalDate randomDate = startDate.plusDays(randomDays);

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return randomDate.format(formatter);
    }

    public static String getEndData(String dateStr, int years) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate currentDate = LocalDate.parse(dateStr, formatter);
        LocalDate futureDate = currentDate.plusYears(years);
        String formattedDate = futureDate.format(formatter);
        return formattedDate;
    }


}
